package com.zhou.common.mybatis.template.shiro;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstanceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
//@Component
public class BPMNCommandLineRunner1 implements CommandLineRunner {


    @Autowired
    private ProcessEngine processEngine;
    @Override
    public void run(String... args) throws Exception {
        //测试businesskey
        TaskService taskService = processEngine.getTaskService();
        FormService formService = processEngine.getFormService();
        HistoryService historyService = processEngine.getHistoryService();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String, Object> variables = new HashMap<String, Object>();
        //设置审批人
        variables.put("test1", "test11");
        variables.put("test1_group", "test1_group1");
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("test1", "case_id_1", variables);

        Task task = taskService.createTaskQuery().taskAssignee("test11").singleResult();
        log.info("task={}",task);
        List<ProcessInstance> case_id_1 = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey("case_id_1").list();
        log.info("tasks={}",case_id_1);
        List<Task> tasks = taskService.createTaskQuery().active().processVariableExists("test1").processVariableValueEquals("test11").list();
        log.info("tasks={}",tasks);
        String processInstanceId="1247bab0-c09a-11ec-b193-5414f3dd8146";
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).list();
        log.info("list={}",list);
        //流程查询
        HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery();
        query.finished();//查询已完成的流程实例
        query.unfinished();//查询已完成的流程实例
        HistoricTaskInstanceQuery taskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
        taskInstanceQuery.finished();//查询已完成的任务
        taskInstanceQuery.unfinished();//查询激活的任务
    }
}
